package com.yicong.ants.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import androidx.appcompat.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.yicong.ants.R;


/**
 * 滚动文字
 */
public class ScrollTextView extends AppCompatTextView {

    private Drawable drawableLeft = null, drawableTop = null, drawableRight = null,
        drawableBottom = null;
    private int drawableWidth, drawableHeight;

    public ScrollTextView(Context context) {
        this(context, null);
    }

    public ScrollTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public ScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScrollTextView);
        int count = typedArray.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.ScrollTextView_scrDrawableRight:
                    drawableRight = typedArray.getDrawable(attr);
                    break;
                case R.styleable.ScrollTextView_scrDrawableLeft:
                    drawableLeft = typedArray.getDrawable(attr);
                    break;
                case R.styleable.ScrollTextView_scrDrawableTop:
                    drawableTop = typedArray.getDrawable(attr);
                    break;
                case R.styleable.ScrollTextView_scrDrawableBottom:
                    drawableBottom = typedArray.getDrawable(attr);
                    break;
                case R.styleable.ScrollTextView_scrDrawableWidth:
                    drawableWidth = typedArray.getDimensionPixelSize(attr, 0);
                    break;
                case R.styleable.ScrollTextView_scrDrawableHeight:
                    drawableHeight = typedArray.getDimensionPixelSize(attr, 0);
                    break;
            }
        }
        if (null != drawableLeft) {
            drawableLeft.setBounds(0, 0, drawableWidth, drawableHeight);
        }
        if (null != drawableRight) {
            drawableRight.setBounds(0, 0, drawableWidth, drawableHeight);
        }
        if (null != drawableTop) {
            drawableTop.setBounds(0, 0, drawableWidth, drawableHeight);
        }
        if (null != drawableBottom) {
            drawableBottom.setBounds(0, 0, drawableWidth, drawableHeight);
        }
        setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
        //设置单行
        setSingleLine();
        //设置Ellipsize
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
        //获取焦点
        setFocusable(true);
        //走马灯的重复次数，-1代表无限重复
        setMarqueeRepeatLimit(-1);
        //强制获得焦点
        setFocusableInTouchMode(true);
    }


    /**
     * 这个属性这个View得到焦点,在这里我们设置为true,这个View就永远是有焦点的
     *
     * @return
     */
    @Override
    public boolean isFocused() {
        return true;
    }

}
